package lumis.portal.report;

import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;

import lumis.portal.PortalException;
import lumis.portal.event.persistence.PortalEventEntityListener;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.stability.StableMinor;

/**
 * A parameter entry of a {@link Report}.
 * 
 * @since 4.2.0
 * @version $Revision: 13082 $ $Date: 2011-05-27 16:03:12 -0300 (Fri, 27 May 2011) $
 */
@StableMinor(version = "6.0", sinceVersion = "4.2")
@Entity
@Table(name="lum_RepParameter")
@GenericGenerator(name="portal-guid", strategy="lumis.portal.dao.hibernate.GuidGenerator")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@EntityListeners(value=PortalEventEntityListener.class)
public class ReportParameter 
{
	@Id @GeneratedValue(generator="portal-guid")
	private String id;
	private String name;
	private String value;
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="reportId")
	private Report report;
	
	/**
	 * Returns the identifier of this parameter.
	 * @return the identifier.
	 * @since 4.2.0
	 */
	public String getId()
	{
		return id;
	}

	/**
	 * Returns the display name for this parameter.
	 * @return the display name.
	 * @since 4.2.0
	 */
	public String getName()
	{
		return name;
	}

	/**
	 * Sets the display name for this parameter.
	 * @param name the display name.
	 * @since 4.2.0
	 */
	public void setName(String name)
	{
		this.name = name;
	}
	
	/**
	 * Returns the value for this parameter.
	 * @return the value for this parameter.
	 * @since 4.2.0
	 */
	public String getValue()
	{
		return value;
	}

	/**
	 * Sets the value for this parameter.
	 * @param value the value.
	 * @since 4.2.0
	 */
	public void setValue(String value)
	{
		this.value = value;
	}
	
	/**
	 * Returns the report this parameter belongs to.
	 * @return the report.
	 * @since 4.2.0
	 */
	public Report getReport()
	{
		return report;
	}

	/**
	 * Sets the report this parameter belongs to.
	 * @param report the report.
	 * @since 4.2.0
	 */
	void setReport(Report report)
	{
		this.report = report;
	}
	
	// -- Operations -- //
	
	/**
	 * Deletes this parameter. This method also removes the parameter from
	 * {@link Report#getParameters()}, for further consistency. 
	 * @since 4.2.0
	 */
	public void delete() throws PortalException
	{
		getReport().removeParameter(this);
		ManagerFactory.getEntityManager().remove(this);
	}
}
